package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.PageHelper;
import com.xbongbong.paas.toolbox.util.CommentUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.WorkOrderErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.WorkOrderPersonAddressPojo;
import com.xbongbong.pro.workorder.pojo.WorkOrderPersonListPojo;
import com.xbongbong.pro.workorder.pojo.WorkOrderPersonListServicePojo;
import com.xbongbong.pro.workorder.pojo.WorkOrderPersonLoactionPojo;
import com.xbongbong.pro.workorder.pojo.WorkOrderPersonUserIdPojo;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonDelDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonEditDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonListDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonSaveDTO;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderPersonServiceListDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonDelVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonEditVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonListVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonSaveVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderPersonServiceListVO;
import com.xbongbong.saas.constant.PageConstant;
import com.xbongbong.saas.constant.workorder.WorkOrderConstant;
import com.xbongbong.saas.domain.entity.WorkOrderPersonEntity;
import com.xbongbong.saas.domain.entity.WorkOrderTagEntity;
import com.xbongbong.saas.model.WorkOrderPersonModel;
import com.xbongbong.saas.model.WorkOrderTagModel;
import com.xbongbong.saas.service.WorkOrderPersonService;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author 刘阳
 * @date 2019/3/15 上午9:54
 * @since v1.0
 * @version v1.0
 */
@Service("WorkOrderPersonService")
public class WorkOrderPersonServiceImpl implements WorkOrderPersonService {

    @Resource
    private WorkOrderPersonModel workOrderPersonModel;

    @Resource
    private WorkOrderTagModel workOrderTagModel;

    @Resource
    private UserModel userModel;

    /**
     * Description:获取公司工单所有服务
     * @Param workOrderPersonListDTO
     * @return workOrderPersonListVO
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午2:35
     * @since v1.0
     */
    @Override
    public WorkOrderPersonListVO getList(WorkOrderPersonListDTO workOrderPersonListDTO) throws XbbException {
        Map<String,Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String corpid = workOrderPersonListDTO.getCorpid();
        Integer serviceId = workOrderPersonListDTO.getService();
        Integer pageSize = 20;
        Integer page = 0;
        if (Objects.equals(workOrderPersonListDTO.getPageSize(),null)){
            pageSize = 20;
        } else {
            pageSize = workOrderPersonListDTO.getPageSize();
            page = workOrderPersonListDTO.getPage();
        }
        String id = workOrderPersonListDTO.getId();

        modelMap.put("corpid", corpid);
        modelMap.put("page", page);
        modelMap.put("pageSize", pageSize);
        modelMap.put("del", 0);

        //根据serviceId的值来判断是初始化还是筛选
        if (!Objects.equals(serviceId,null) && !Objects.equals(serviceId,0)) {
            modelMap.put("tagIdJsonLike", serviceId);
        }
        CommentUtil.addToMap(modelMap,"userId", id);
        Map<String, Object> param = modelMap;
        Integer rowCounts = workOrderPersonModel.getEntitysCount(param);
        Integer pages = (Integer) param.get("page");
        if (pages == null || pages < 1) {
            pages = 1;
        }
        param.put("pages", pages);
        //记录总数放入param
        param.put("total", rowCounts);

        PageHelper pageHelper = initPageHelper(rowCounts, page, pageSize).setPageUriStr(CommentUtil.mapToUrl(param));

        param.put("pageNum", pageHelper.getPageSize());
        param.put("start", (pageHelper.getCurrentPageNum() - 1) * pageHelper.getPageSize());
        List<WorkOrderPersonEntity> workOrderPersonEntityList = workOrderPersonModel.findEntitys(param);

        modelMap.clear();
        modelMap.put("corpid", corpid);
        modelMap.put("del", 0);
        List<WorkOrderTagEntity> tagList = workOrderTagModel.findEntitys(modelMap);

        Map<Long,WorkOrderTagEntity> serviceMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (WorkOrderTagEntity entity : tagList){
            serviceMap.put(entity.getId(),entity);
        }

        List<WorkOrderPersonListPojo> entityPojos = new ArrayList<>();
        for (WorkOrderPersonEntity entity : workOrderPersonEntityList){
            WorkOrderPersonListPojo workOrderPersonListPojo = new WorkOrderPersonListPojo();
            StringBuilder address = new StringBuilder();
            address.append(entity.getProvince())
                    .append(entity.getCity())
                    .append(entity.getDistrict())
                    .append(entity.getAddress());
            workOrderPersonListPojo.setId(entity.getId());
            workOrderPersonListPojo.setAddress(address.toString());
            workOrderPersonListPojo.setName(entity.getUserName());
            workOrderPersonListPojo.setService(getService(entity.getTagId(),serviceMap,serviceId));
            workOrderPersonListPojo.setAvatar(entity.getUserAvatar());

            entityPojos.add(workOrderPersonListPojo);
        }
        WorkOrderPersonListVO workOrderPersonListVO = new WorkOrderPersonListVO();
        workOrderPersonListVO.setTemplateList(entityPojos);
        workOrderPersonListVO.setPageHelper(pageHelper);
        return workOrderPersonListVO;

    }
    /**
     * Description:服务标签转换，筛选标签默认顺序第一
     * @Param  tagId 服务标签id
     * @return
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午2:06
     * @since v1.0
     */
    private List<WorkOrderPersonListServicePojo> getService(String tagId, Map<Long,WorkOrderTagEntity> serviceMap, Integer serviceId){
        List<Long> idIn = JSONArray.parseArray(tagId, Long.class);
        List<WorkOrderPersonListServicePojo> servicePojoList = new ArrayList<>();
        for (Long id : idIn){
            WorkOrderPersonListServicePojo servicePojo = new WorkOrderPersonListServicePojo();
            if (serviceMap.containsKey(id)){
                servicePojo.setId(id);
                servicePojo.setValue(serviceMap.get(id).getName());
            }
                servicePojoList.add(servicePojo);
        }
        return servicePojoList;
    }
    /**
     * Description:初始化pageHelper
     * @Param
     * @return PageHelper
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午2:36
     * @since v1.0
     */
    public static PageHelper initPageHelper(Integer rowCounts, Integer page, Integer pageSize) {
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = PageConstant.DEFAULT_PAGE_SIZE;
        }
        return new PageHelper(page, pageSize).setRowsCount(rowCounts);
    }
    /**
     * Description:获取公司所有工单服务名称
     * @Param workOrderPersonServiceListDTO
     * @return workOrderPersonServiceListVO
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午2:37
     * @since v1.0
     */
    @Override
    public WorkOrderPersonServiceListVO getServiceAjax(WorkOrderPersonServiceListDTO workOrderPersonServiceListDTO)throws XbbException{
        Map<String,Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String corpid = workOrderPersonServiceListDTO.getCorpid();
        String type = workOrderPersonServiceListDTO.getType();
        Integer pageSize = 20;
        Integer page = 0;
        if (Objects.equals(workOrderPersonServiceListDTO.getPageSize(),null)) {
            pageSize = 20;
        } else {
            pageSize = workOrderPersonServiceListDTO.getPageSize();
            page = workOrderPersonServiceListDTO.getPage();
        }
        String nameLike = workOrderPersonServiceListDTO.getNameLike();

        CommentUtil.addToMap(modelMap,"nameLike",nameLike);
        modelMap.put("corpid", corpid);
        modelMap.put("del", 0);
        modelMap.put("orderByStr", "num desc");
        modelMap.put("page", page);
        if (Objects.equals(type,"new") || Objects.equals(type,"list")){
            modelMap.put("minNum", "num > 0");
        }

        Map<String, Object> param = modelMap;
        Integer rowCounts = workOrderTagModel.getEntitysCount(param);
        Integer pages = (Integer) param.get("page");
        if (pages == null || pages < 1) {
            pages = 1;
        }
        param.put("pages", pages);
        //记录总数放入param
        param.put("total", rowCounts);

        PageHelper pageHelper = initPageHelper(rowCounts, page, pageSize).setPageUriStr(CommentUtil.mapToUrl(param));

        List<WorkOrderTagEntity> tagList = workOrderTagModel.findEntitys(param);

        List<WorkOrderPersonListServicePojo> workOrderPersonListServicePojos = new ArrayList<>();
        for (WorkOrderTagEntity entity : tagList){
            WorkOrderPersonListServicePojo workOrderPersonListServicePojo = new WorkOrderPersonListServicePojo();
            workOrderPersonListServicePojo.setId(entity.getId());
            workOrderPersonListServicePojo.setValue(entity.getName());
            workOrderPersonListServicePojos.add(workOrderPersonListServicePojo);
        }
        WorkOrderPersonServiceListVO workOrderPersonServiceListVO = new WorkOrderPersonServiceListVO();
        workOrderPersonServiceListVO.setServiceList(workOrderPersonListServicePojos);
        workOrderPersonServiceListVO.setPageHelper(pageHelper);
        return workOrderPersonServiceListVO;
    }
    /**
     * Description:工单服务人员编辑
     * @Param workOrderPersonEditDTO
     * @return workOrderPersonEditVO
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午2:38
     * @since v1.0
     */
    @Override
    public WorkOrderPersonEditVO getEdit(WorkOrderPersonEditDTO workOrderPersonEditDTO)throws XbbException{
        Map<String,Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String corpid = workOrderPersonEditDTO.getCorpid();
        Long id = workOrderPersonEditDTO.getId();
        if (id<=0){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        WorkOrderPersonEntity entity = workOrderPersonModel.getByKey(id,corpid);
        if (entity == null || entity.getDel() == 1){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100004);
        }
        modelMap.put("corpid", corpid);
        modelMap.put("del", 0);
        List<WorkOrderTagEntity> tagList = workOrderTagModel.findEntitys(modelMap);

        Map<Long,WorkOrderTagEntity> serviceMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (WorkOrderTagEntity tag : tagList){
            serviceMap.put(tag.getId(),tag);
        }
        WorkOrderPersonEditVO workOrderPersonEditVO = new WorkOrderPersonEditVO();
        workOrderPersonEditVO.setDel(entity.getDel());

        WorkOrderPersonAddressPojo workOrderPersonAddressPojo = new WorkOrderPersonAddressPojo();
        workOrderPersonAddressPojo.setLocation(new WorkOrderPersonLoactionPojo(entity.getLongitude(), entity.getLatitude()));
        workOrderPersonAddressPojo.setAddress(entity.getAddress());
        workOrderPersonAddressPojo.setProvince(entity.getProvince());
        workOrderPersonAddressPojo.setDistrict(entity.getDistrict());
        workOrderPersonAddressPojo.setCity(entity.getCity());

        workOrderPersonEditVO.setAddress(workOrderPersonAddressPojo);

        WorkOrderPersonUserIdPojo workOrderPersonUserIdPojo = new WorkOrderPersonUserIdPojo();
        workOrderPersonUserIdPojo.setUserId(entity.getUserId());
        workOrderPersonUserIdPojo.setUserName(entity.getUserName());

        workOrderPersonEditVO.setUserId(workOrderPersonUserIdPojo);

        workOrderPersonEditVO.setService(getService(entity.getTagId(),serviceMap,0));

        return workOrderPersonEditVO;
    }
    /**
     * Description:工单服务人员保存
     * @param workOrderPersonSaveDTO
     * @return workOrderPersonSaveVO
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午2:04
     * @since v1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkOrderPersonSaveVO save(WorkOrderPersonSaveDTO workOrderPersonSaveDTO)throws XbbException{
        String corpid = workOrderPersonSaveDTO.getCorpid();
        WorkOrderPersonUserIdPojo serviceHumanId ;
        WorkOrderPersonAddressPojo addressPojo ;
        if (Objects.equals(workOrderPersonSaveDTO.getServiceHumanId(),"")) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        serviceHumanId = workOrderPersonSaveDTO.getServiceHumanId();
        String userId = serviceHumanId.getUserId();
        String userName = serviceHumanId.getUserName();
        String userAvatar = "";
        Long id = workOrderPersonSaveDTO.getId();
        //service 非空判断
        if (Objects.equals(workOrderPersonSaveDTO.getService(),"")) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        //服务标签大小判断
        if (workOrderPersonSaveDTO.getService().size()> WorkOrderConstant.PERSON_HAS_TAG){
            throw new XbbException(WorkOrderErrorCodeEnum.API_ERROR_222040.getCode(), String.format(WorkOrderErrorCodeEnum.API_ERROR_222040.getMsg(), WorkOrderConstant.PERSON_HAS_TAG));
        }
        //服务标签
        Set<Long> idList = new HashSet<>();
        for (WorkOrderPersonListServicePojo pojo : workOrderPersonSaveDTO.getService()) {
            Long ids = pojo.getId();
            if (!Objects.equals(ids, 0L)) {
                idList.add(ids);
            }
        }
        //人员选择
        if ("".equals(userId)) {
            throw new XbbException(WorkOrderErrorCodeEnum.API_ERROR_222002);
        }
        UserEntity user = userModel.getByKey(userId,corpid);
        //头像照片
        if (user!=null) {
            userAvatar = user.getAvatar() == null?"":user.getAvatar();
        }
        //判断地址集合是否为空
        if (Objects.equals(workOrderPersonSaveDTO.getAddress(),"")) {
            throw new XbbException(WorkOrderErrorCodeEnum.API_ERROR_222029);
        }
        addressPojo = workOrderPersonSaveDTO.getAddress();
        String province = addressPojo.getProvince();
        String city = addressPojo.getCity();
        String district = addressPojo.getDistrict();
        String address = addressPojo.getAddress();
        WorkOrderPersonLoactionPojo location = addressPojo.getLocation();
        //具体定位位置
        Double longitude;
        Double latitude;
        if (Objects.isNull(location)) {
            longitude = 0D;
            latitude = 0D;
        } else {
            longitude = location.getLon();
            latitude = location.getLat();
            if (longitude == null) {
                longitude = 0D;
            }
            if (latitude == null){
                latitude = 0D;
            }
        }
        //实体保存
        WorkOrderPersonEntity workOrderPersonEntity;
        Long now = DateTimeUtil.getInt();
        Boolean isNew = false;
        if (id <= 0) {
            //新增
            isNew = true;
            workOrderPersonEntity = new WorkOrderPersonEntity();
            workOrderPersonEntity.setAddTime(now);
            workOrderPersonEntity.setCorpid(corpid);
            workOrderPersonEntity.setCreatorId(userId);
            workOrderPersonEntity.setCreatorName(userName);
            workOrderPersonEntity.setDel(0);
        }else {
            workOrderPersonEntity = workOrderPersonModel.getByKey(id,corpid);
            if (workOrderPersonEntity == null || workOrderPersonEntity.getDel() == 1){
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100404);
            }
        }
        //经纬度判断
        //经纬度超出范围时：经度使用最大值180度,纬度使用最大90度或最小值-90度
        if (longitude >= PaasConstant.LONGITUDE_MAX) {
            longitude = 180D;
        } else if (longitude <= PaasConstant.LONGITUDE_MIN) {
            longitude = -180D;
        }
        if (latitude >= PaasConstant.LATITUDE_MAX) {
            latitude = 90D;
        } else if (latitude <= PaasConstant.LATITUDE_MIN) {
            latitude = -90D;
        }

        workOrderPersonEntity.setUserId(userId);
        workOrderPersonEntity.setUserName(userName);
        workOrderPersonEntity.setUserAvatar(userAvatar);
        workOrderPersonEntity.setProvince(province);
        workOrderPersonEntity.setCity(city);
        workOrderPersonEntity.setDistrict(district);
        workOrderPersonEntity.setAddress(address);
        workOrderPersonEntity.setLongitude(longitude);
        workOrderPersonEntity.setLatitude(latitude);
        workOrderPersonEntity.setModifierId(userId);
        workOrderPersonEntity.setModifierName(userName);
        workOrderPersonEntity.setUpdateTime(now);

        Map<String,Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        modelMap.put("corpid", corpid);
        modelMap.put("del", 0);
        List<WorkOrderTagEntity> tagList = workOrderTagModel.findEntitys(modelMap);
        Map<Long, WorkOrderTagEntity> tagMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (WorkOrderTagEntity tag : tagList){
            tagMap.put(tag.getId(), tag);
        }
        Set<Long> newIds;
        if (isNew) {
            //已有标签num+1,新增标签添加
            try {
                newIds = plusTagNum(workOrderPersonSaveDTO.getService(), tagMap, corpid, isNew, null);
            } catch (XbbException e){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw e;
            }
        } else {
            //删除标签num-1
            List<Long> keepIdIn = minusTagNum(idList.toString(), id, corpid);
            //已有标签num+1,新增标签添加
            try {
                newIds = plusTagNum(workOrderPersonSaveDTO.getService(), tagMap, corpid, isNew, keepIdIn);
            } catch (XbbException e) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw e;
            }
        }
        idList.addAll(newIds);
        workOrderPersonEntity.setTagId(idList.toString());

        //todo 日志

        workOrderPersonModel.save(workOrderPersonEntity);
        WorkOrderPersonSaveVO workOrderPersonSaveVO = new WorkOrderPersonSaveVO();
        workOrderPersonSaveVO.setId(workOrderPersonEntity.getId());
        return workOrderPersonSaveVO;

    }
    /**
     * Description: 工单服务人员删除
     * @Param workOrderPersonDelDTO
     * @return workOrderPersonDelVO
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午3:41
     * @since v1.0
     */
    @Override
    public WorkOrderPersonDelVO delete(WorkOrderPersonDelDTO workOrderPersonDelDTO)throws XbbException{
        Map<String,Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        String corpid = workOrderPersonDelDTO.getCorpid();
        Long id = workOrderPersonDelDTO.getId();
        if (id == 0){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100002);
        }
        WorkOrderPersonEntity entity = workOrderPersonModel.getByKey(id,corpid);
        if (entity == null || entity.getDel() == 1){
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
        }
        List<String> serviceList = new ArrayList<>();
        serviceList.add(entity.getTagId());
        workOrderPersonModel.deleteByKey(id,corpid);
        //更新服务标签表
        Map<Integer,Integer> minusNumMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Set<Integer> minusIdIn = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (String tagId : serviceList){
            List<Integer> serviceIdIn = JSONArray.parseArray(tagId, Integer.class);
            for (Integer serviceId : serviceIdIn){
                if (minusNumMap.containsKey(serviceId)){
                    minusNumMap.put(serviceId,minusNumMap.get(serviceId) + 1);
                }else {
                    minusNumMap.put(serviceId,1);
                }
                minusIdIn.add(serviceId);
            }
        }
        if (!minusIdIn.isEmpty()) {
            modelMap.put("corpid", corpid);
            modelMap.put("del", 0);
            modelMap.put("idIn", minusIdIn);
            workOrderTagModel.minusNum(modelMap,minusNumMap);
            modelMap.clear();
        }
        WorkOrderPersonDelVO workOrderPersonDelVO = new WorkOrderPersonDelVO();
        return workOrderPersonDelVO;
    }
    /**
     * Description:工单服务人员标签增加
     * @Param workOrderPersonListServicePojos
     * @return plusIdIn
     * @throws XbbException
     * @author 刘阳
     * @date 2019/3/22 下午3:43
     * @since v1.0
     */
    private Set<Long> plusTagNum(List<WorkOrderPersonListServicePojo> workOrderPersonListServicePojos, Map<Long, WorkOrderTagEntity> tagMap, String corpid, Boolean isNew, List<Long> keepIdIn)throws XbbException{
        Map<String,Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Set<Long> plusIdIn = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        Integer count = tagMap.size();
        //已有标签num+1,新增标签添加
        for (WorkOrderPersonListServicePojo pojo : workOrderPersonListServicePojos) {
            String tagName = pojo.getValue();
            Long tagId = pojo.getId();
            if (tagName.length() == 0) {
                continue;
            }
            WorkOrderTagEntity tagEntity = tagMap.get(tagId);
            if (tagEntity != null) {
                if (!isNew){
                    if (keepIdIn != null && !keepIdIn.contains(tagEntity.getId())){
                        plusIdIn.add(tagEntity.getId());
                    }
                }else {
                    plusIdIn.add(tagEntity.getId());
                }
            }else{
                if (count >= WorkOrderConstant.TAG_MAX_NUM){
                    throw new XbbException(WorkOrderErrorCodeEnum.API_ERROR_222041.getCode(), String.format(WorkOrderErrorCodeEnum.API_ERROR_222041.getMsg(), WorkOrderConstant.TAG_MAX_NUM));
                }
                Integer currentTime = DateUtil.getInt();
                tagEntity = new WorkOrderTagEntity();
                tagEntity.setCorpid(corpid);
                tagEntity.setName(tagName);
                tagEntity.setNum(0);
                tagEntity.setAddTime(currentTime);
                tagEntity.setUpdateTime(currentTime);
                tagEntity.setDel(0);

                workOrderTagModel.save(tagEntity);

                plusIdIn.add(tagEntity.getId());
                count ++;
            }
        }
        if (plusIdIn.size() > 0) {
            Integer currentTime = DateUtil.getInt();
            modelMap.clear();
            modelMap.put("corpid", corpid);
            modelMap.put("minusIdIn", plusIdIn);
            modelMap.put("operation", "num+1");
            modelMap.put("idIn", plusIdIn);
            modelMap.put("newUpdateTime", currentTime);
            modelMap.put("del", 0);
            workOrderTagModel.numOperation(modelMap);
        }
        return plusIdIn;
    }
    /**
     * Description:工单服务人员标签减少
     * @Param
     * @return serviceIdIn
     * @author 刘阳
     * @date 2019/3/22 下午2:38
     * @since
     */
    private List<Long> minusTagNum(String service, Long id, String corpid) {
        Map<String,Object> modelMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        WorkOrderPersonEntity personEntity = workOrderPersonModel.getByKey(id, corpid);
        String serviceIds = personEntity.getTagId();
        //原有的
        List<Long> serviceIdIn = JSONArray.parseArray(serviceIds, Long.class);
        if (serviceIdIn == null){
            serviceIdIn = new ArrayList<>();
        }
        //现有的
        List<Long> nowServiceIdIn = JSONArray.parseArray(service, Long.class);
        //删除的
        List<Long> delIdIn = new ArrayList<>(serviceIdIn);
        delIdIn.removeAll(nowServiceIdIn);
        //不变的
        serviceIdIn.removeAll(delIdIn);

        if (delIdIn.size() > 0) {
            modelMap.clear();
            modelMap.put("corpid", corpid);
            modelMap.put("minusIdIn", delIdIn);
            modelMap.put("operation", "num-1");
            modelMap.put("idIn", delIdIn);
            modelMap.put("del", 0);
            workOrderTagModel.numOperation(modelMap);
        }

        return serviceIdIn;
    }

    @Override
    public List<WorkOrderPersonEntity> getPersonListByUser(String corpid, String userId) throws XbbException {
        Map<String,Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid",corpid);
        param.put("del",0);
        param.put("userId",userId);
        return workOrderPersonModel.findEntitys(param);
    }
}
